package  org.ttitfly.ttcore.util.query;

import static org.ttitfly.ttcore.util.query.SQueryBuilder.PRE_SEPARATOR;
import static org.ttitfly.ttcore.util.query.SQueryBuilder.POST_SEPARATOR;
public class SQLBuilder {
    StringBuilder pattern = new StringBuilder("");

    public SQLBuilder(String sql) {
        pattern.append(" ");
        pattern.append(sql);
    }

    public void append(String sql) {
        if (isClause(sql)) {
            appendClause(sql);
            return;
        }
        pattern.append(" ");
        pattern.append(sql);
    }

    public void appendDirectly(String sql) {
        pattern.append(sql);
    }

    public void appendClause(String clause) {
        pattern.append(" ");
        pattern.append(PRE_SEPARATOR);
        pattern.append(" ");
        pattern.append(clause);
        pattern.append(" ");
        pattern.append(POST_SEPARATOR);
    }

    public String getSql() {
        return pattern.toString();
    }

    @Override
    public String toString() {

        return pattern.toString();
    }

    private boolean isClause(String sql) {
        if (sql.trim().toLowerCase().startsWith("and") && sql.indexOf(":") > 0) {
            return true;
        }
        return false;
    }

}
